<template>
    <div class="order-add">
            <el-form :model="eleForm" :rules="rules" label-width="80px" size="medium" ref="eleForm" @keyup.enter.native="onSubmit">
                <el-row>
                    <el-form-item label="商品ID" prop="goodsID">
                        <el-input v-model.trim="eleForm.goodsID" style="width:200px" placeholder="请输入商品ID"></el-input>
                    </el-form-item>
                    <el-form-item label="用户ID" prop="userID">
                        <el-input v-model.trim="eleForm.userID" style="width:200px" placeholder="请输入用户ID"></el-input>
                    </el-form-item>
                    <el-form-item label="数量" prop="buyNum">
                        <el-input v-model.trim="eleForm.buyNum" style="width:200px" placeholder="请输入数量"></el-input>
                    </el-form-item>
                </el-row>
            </el-form>
        <div style="text-align:center;margin-top:20px">
            <el-button @click="escClick">取消</el-button>
            <el-button type="primary" @click="onSubmit" :loading="submitLoading">提交</el-button>
        </div>
    </div>
</template>
<script>
// 发请求全部导入
import api from '@/api/order/index'
// 工具
import tool from '@/utils/tool'
export default {
    props:['dialogInfo'],
    data() {
        return {
            // 提交时避免重复点击
            submitLoading:false,

            eleForm:{
                userID:'',
                goodsID:'',
                buyNum:'',
            },
            rules:{
                userID:[
                    {required:true,message:'请输入用户ID',trigger:'blur'},
                ],
                goodsID:[
                    {required:true,message:'请输入商品ID',trigger:'blur'},
                ],
                buyNum:[
                    {required:true,message:'请输入数量',trigger:'blur'},
                    {pattern:/^[1-9]+[0-9]*$/,message:'只能输入正整数',trigger:'blur'}
                ],
            },
        }
    },
    methods: {
        // 取消
        escClick(){
            this.$emit('dialogCancel')
        },
        // 提交
        onSubmit(){
            // 更新
            if(this.dialogInfo){
                this.$refs.eleForm.validate(valid=>{
                    if(!valid) return
                    // console.log(this.eleForm)
                    this.submitLoading = true
                    api.updateOrder(this.eleForm).then(res=>{
                        this.submitLoading = false
                        if(res.code !==200 ) return
                        console.log(res)
                        this.$message.success('更新成功!')
                        this.$emit('dialogSubmit')
                    })
                })
            // 新增
            }else{
                this.$refs.eleForm.validate(valid=>{
                    if(!valid) return
                    console.log(this.eleForm)
                    this.submitLoading = true
                    api.addOrder(this.eleForm).then(res=>{
                        this.submitLoading = false
                        if(res.code !==200 ) return
                        console.log(res)
                        this.$message.success('添加成功!')
                        this.$emit('dialogSubmit')
                    })
                })
            }
        },
    },
    created() {
        console.log(this.dialogInfo)
        if(this.dialogInfo){
            // 如果有,那就是更新操作(执行深拷贝，不然会影响父组件的数据------对象的传递传递的是引用)
            this.eleForm = JSON.parse(JSON.stringify(this.dialogInfo))
            // 特殊处理
            this.eleForm.userID = this.eleForm.userID._id
            this.eleForm.goodsID = this.eleForm.goodsID._id
        }
    },
    mounted() {
    },
}
</script>
<style lang="less" scoped>

</style>